package com.whl.sm.controller.system;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.whl.sm.common.result.Result;
import com.whl.sm.controller.BaseController;
import com.whl.sm.dto.CascaderDto;
import com.whl.sm.dto.SelectDto;
import com.whl.sm.model.system.Role;
import com.whl.sm.service.system.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author wuhaolun
 * @since 2021-09-10
 */
@RestController
@RequestMapping("/role")
public class RoleController extends BaseController {

    @Autowired
    RoleService roleService;

    @RequestMapping("/list")
    public Result getRoleList(HttpServletRequest request){
        QueryWrapper queryWrapper = getLikeParam("role_name");
        Page<Role> rolePage = roleService.page(getPage(),queryWrapper);
        return Result.ok(rolePage);
    }

    @RequestMapping("/tree")
    public Result  getRoleTree(){
        Map<String,Object> result = new HashMap<>();
        result.put("roleTree",convertToTree(roleService.getRoleTypeList()));
        return Result.ok(result);
    }

    @RequestMapping("/wxList")
    public Result  getRoleChooseList(){
        List<SelectDto> resultDto = new ArrayList<>();
        List<Role> resultList = roleService.list();
        if (resultList.size()>0){
            resultDto = resultList.stream().map(obj->{
                return new SelectDto(obj.getId(),obj.getRoleName(),null,false);
            }).collect(Collectors.toList());
        }
        return Result.ok(resultDto);
    }


    @RequestMapping("/info/{roleId}")
    public Result getRoleList(@PathVariable("roleId") String roleId){

        return Result.ok(roleService.getById(roleId));
    }

    @RequestMapping("/info/update")
    public Result updateRole(@RequestBody Role role){
        roleService.update(role);
        return Result.ok();
    }

    @RequestMapping("/delete")
    public Result delete(@RequestBody List<String> idList){
        if (idList.size()>1){
            roleService.deleteByList(idList);
        }
        else {
            roleService.removeById(idList.get(0));
        }
        return Result.ok();
    }

    //练级选择器树结构转换
    private List<CascaderDto> convertToTree(List<Role> role) {
        List<CascaderDto> cascaderDto = new ArrayList<>();
        role.forEach(m -> {
            CascaderDto dto = new CascaderDto();
            dto.setValue(m.getId());
            dto.setLabel(m.getRoleName());
            if (m.getChildren().size() > 0) {
                dto.setChildren(convertToTree(m.getChildren()));
            }
            else {
                dto.setChildren(null);
            }
            cascaderDto.add(dto);
        });
        return cascaderDto;
    }


}
